package ch.quantasy.tinkerforge.tinker;

import com.tinkerforge.BrickDC;
import com.tinkerforge.BrickIMU;
import com.tinkerforge.BrickMaster;
import com.tinkerforge.BrickServo;
import com.tinkerforge.BrickStepper;
import com.tinkerforge.BrickletAmbientLight;
import com.tinkerforge.BrickletAnalogIn;
import com.tinkerforge.BrickletAnalogOut;
import com.tinkerforge.BrickletBarometer;
import com.tinkerforge.BrickletCurrent12;
import com.tinkerforge.BrickletCurrent25;
import com.tinkerforge.BrickletDistanceIR;
import com.tinkerforge.BrickletDualRelay;
import com.tinkerforge.BrickletGPS;
import com.tinkerforge.BrickletHumidity;
import com.tinkerforge.BrickletIO16;
import com.tinkerforge.BrickletIO4;
import com.tinkerforge.BrickletIndustrialDigitalIn4;
import com.tinkerforge.BrickletIndustrialDigitalOut4;
import com.tinkerforge.BrickletIndustrialQuadRelay;
import com.tinkerforge.BrickletJoystick;
import com.tinkerforge.BrickletLCD16x2;
import com.tinkerforge.BrickletLCD20x4;
import com.tinkerforge.BrickletLinearPoti;
import com.tinkerforge.BrickletPiezoBuzzer;
import com.tinkerforge.BrickletRotaryPoti;
import com.tinkerforge.BrickletTemperature;
import com.tinkerforge.BrickletTemperatureIR;
import com.tinkerforge.BrickletVoltage;
import com.tinkerforge.BrickletVoltageCurrent;
import com.tinkerforge.Device;
/**
 * This enum is used as a convenience to map the Tinker-Bricks and Tinker-Bricklets
 * It tries to hide the 'strange' stuff from the Java-Developer ;-) 
 * @author reto
 *
 */
public enum TinkerforgeDevice {
	AmbientLight(BrickletAmbientLight.class,
			BrickletAmbientLight.DEVICE_IDENTIFIER), Master(BrickMaster.class,
			BrickMaster.DEVICE_IDENTIFIER), Humidity(BrickletHumidity.class,
			BrickletHumidity.DEVICE_IDENTIFIER), LCD20x4(BrickletLCD20x4.class,
			BrickletLCD20x4.DEVICE_IDENTIFIER), AnalogIn(
			BrickletAnalogIn.class, BrickletAnalogIn.DEVICE_IDENTIFIER), AnalogOut(
			BrickletAnalogOut.class, BrickletAnalogOut.DEVICE_IDENTIFIER), Barometer(
			BrickletBarometer.class, BrickletBarometer.DEVICE_IDENTIFIER), DC(
			BrickDC.class, BrickDC.DEVICE_IDENTIFIER), IMU(BrickIMU.class,
			BrickIMU.DEVICE_IDENTIFIER), Current12(BrickletCurrent12.class,
			BrickletCurrent12.DEVICE_IDENTIFIER), Current25(
			BrickletCurrent25.class, BrickletCurrent25.DEVICE_IDENTIFIER), DistanceIR(
			BrickletDistanceIR.class, BrickletDistanceIR.DEVICE_IDENTIFIER), DualRelay(
			BrickletDualRelay.class, BrickletDualRelay.DEVICE_IDENTIFIER), GPS(
			BrickletGPS.class, BrickletGPS.DEVICE_IDENTIFIER), IndustrialDigitalIn4(
			BrickletIndustrialDigitalIn4.class,
			BrickletIndustrialDigitalIn4.DEVICE_IDENTIFIER), IndustrialDigitalOut4(
			BrickletIndustrialDigitalOut4.class,
			BrickletIndustrialDigitalOut4.DEVICE_IDENTIFIER), IndustrialQuadRelay(
			BrickletIndustrialQuadRelay.class,
			BrickletIndustrialQuadRelay.DEVICE_IDENTIFIER), IO16(
			BrickletIO16.class, BrickletIO16.DEVICE_IDENTIFIER), IO4(
			BrickletIO4.class, BrickletIO4.DEVICE_IDENTIFIER), Joystick(
			BrickletJoystick.class, BrickletJoystick.DEVICE_IDENTIFIER), LCD16x2(
			BrickletLCD16x2.class, BrickletLCD16x2.DEVICE_IDENTIFIER), LinearPoti(
			BrickletLinearPoti.class, BrickletLinearPoti.DEVICE_IDENTIFIER), PiezoBuzzer(
			BrickletPiezoBuzzer.class, BrickletPiezoBuzzer.DEVICE_IDENTIFIER), RotaryPoti(
			BrickletRotaryPoti.class, BrickletRotaryPoti.DEVICE_IDENTIFIER), Temperature(
			BrickletTemperature.class, BrickletTemperature.DEVICE_IDENTIFIER), TemperatureIR(
			BrickletTemperatureIR.class,
			BrickletTemperatureIR.DEVICE_IDENTIFIER), Voltage(
			BrickletVoltage.class, BrickletVoltage.DEVICE_IDENTIFIER), VoltageCurrent(
			BrickletVoltageCurrent.class,
			BrickletVoltageCurrent.DEVICE_IDENTIFIER), Servo(BrickServo.class,
			BrickServo.DEVICE_IDENTIFIER), Stepper(BrickStepper.class,
			BrickStepper.DEVICE_IDENTIFIER);

	public final int identifier;
	public final Class<?> device;

	private TinkerforgeDevice(Class<?> device, int identifier) {
		if(device==null) throw new IllegalArgumentException();
		this.device = device;
		this.identifier = identifier;
	}

	public static TinkerforgeDevice getDevice(Device device) {
		if(device==null) throw new IllegalArgumentException();
		Class deviceClass = device.getClass();
		for (TinkerforgeDevice tinkerforgeDevice : TinkerforgeDevice.values()) {
			if (deviceClass == tinkerforgeDevice.device)
				return tinkerforgeDevice;
		}
		return null;
	}

	public static TinkerforgeDevice getDevice(int deviceIdentifier) {
		for (TinkerforgeDevice tinkerforgeDevice : TinkerforgeDevice.values()) {
			if (deviceIdentifier == tinkerforgeDevice.identifier)
				return tinkerforgeDevice;
		}
		return null;
	}

}
